package com.jumi.microservice.order.handler;

import com.jumi.microservice.common.redis.RedisCache;
import com.jumi.microservice.enumerate.FreeGoodsEnum;
import com.jumi.microservice.order.context.GoodsSkuContext;
import com.jumi.microservice.order.context.OrderCreateContext;
import com.jumi.microservice.order.context.TransHandlerContext;
import com.jumi.microservice.rocketmq.AutoSpiltOrderListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wangjingdao
 */
@Component
public class ActivityHandler implements TransHandler {
    private static final Logger log = LoggerFactory.getLogger(ActivityHandler.class);
    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handler(TransHandlerContext context) {
        log.info("开始验证是否是付邮领 商品");
        OrderCreateContext ctx = (OrderCreateContext) context;
        List<GoodsSkuContext> goodsSkuContextList = ctx.getGoodsList();
        goodsSkuContextList.forEach(goodsSkuContext -> {
            Object cacheObject = redisCache.getCacheObject("PostageGoods::" + goodsSkuContext.getSpuId());
            log.info(cacheObject != null ? "是付邮领商品" : "不是付邮领 商品" + goodsSkuContext.getSpuId()+"是spuId");
            goodsSkuContext.setFreeGoods(cacheObject != null ? FreeGoodsEnum.YES_FREE.getCode() : FreeGoodsEnum.NO_FREE.getCode());
        });
        return true;
    }
}
